
import * as types from './mutation-types'
import http from '@/utils/HttpClient'
import globalData from '@/utils/GlobalData'

function currentTasksKey () {
  return tasksKey(globalData.appUser.year, globalData.appUser.quarter)
}

function tasksKey (year, quarter) {
  return year.toString() + quarter.toString()
}

// root state object.
// each Vuex instance is just a single state tree.
const state = {
  studentInfo: {},
  tasks: {}, // 根据季节和年作为key的一个词典
  groups: null, // 根据季节分类的任务组
  ranks: null
}

// mutations are operations that actually mutates the state.
// each mutation handler gets the entire state tree as the
// first argument, followed by additional payload arguments.
// mutations must be synchronous and can be recorded by plugins
// for debugging purposes.
const mutations = {
  [types.LOAD_STUDENT_INFO] (_state, data) {
    _state.studentInfo = data
  },
  [types.LOAD_STUDY_TASK] (_state, data) {
    var tasks = {}
    Object.assign(tasks, _state.tasks)
    tasks[data.key] = data.value
    _state.tasks = tasks
  },
  [types.LOAD_HISTORY_TASK] (_state, data) {
    _state.groups = data
  },
  [types.LOAD_STUDENT_RANK] (_state, data) {
    _state.ranks = data
  }
}

// actions are functions that cause side effects and can involve
// asynchronous operations.
const actions = {
  async createStudentTask ({ commit, state }, task) {
    var data = await http.post({ url: 'studyTask', data: task })
    var key = tasksKey(data.year, data.quarter)
    var tasks = state.tasks[key]
    tasks.items.push(data)
    return data
  },

  updateStudentTaskStatus ({ commit, state }, { taskId, year, quarter, status }) {
    var key = tasksKey(year, quarter)
    var tasks = state.tasks[key]
    var task = tasks.items.find(a => a.id === taskId)
    task.status = status
  },
  async getStudentInfoAsync ({ commit }) {
    var data = await http.get({ url: 'student/studentInfo' })
    commit(types.LOAD_STUDENT_INFO, data)
  },
  async getHistoryTaskAsync ({ commit }) {
    var data = await http.get({ url: 'studyTask/history/' + globalData.appUser.id })
    commit(types.LOAD_HISTORY_TASK, data)
  },

  async getStudyTaskByQuarter ({ commit }, { year, quarter }) {
    var data = await http.get({ url: 'studyTask/tasks/' + globalData.appUser.id,
      data: {
        year: year,
        quarter: quarter
      } })
    commit(types.LOAD_STUDY_TASK, { key: tasksKey(year, quarter), value: data })
  },
  async getStudentRank ({ commit, state }) {
    if (state.ranks && state.ranks.items.length === state.ranks.totalCount) {
      return
    }
    var data = await http.get({ url: 'rank/studentRank',
      data: {
        SkipCount: state.ranks == null ? 0 : state.ranks.items.length,
        MaxResultCount: 20
      } })
    if (state.ranks !== null) {
      var items = state.ranks.items.concat(data.items)
      data.items = items
    }
    commit(types.LOAD_STUDENT_RANK, data)
  }
}

// getters are functions
const getters = {
  currentTasks: _state => _state.tasks ? _state.tasks[currentTasksKey()] : null,
  getTasksByQuarter: (_state) => (year, quarter) => {
    var r = _state.tasks[tasksKey(year, quarter)]
    return r || null
  },
  getGroupByQuarter: (_state) => (year, quarter) => {
    return _state.groups.items.find(e => {
      return e.year === year && e.quarter === quarter
    })
  }
}

// A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default {
  state,
  getters,
  actions,
  mutations
}
